package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.MPayServiceMapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.PayService;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.PayServiceDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServiceInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServicedInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchPayServiceInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IPayServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PayServiceServiceImpl implements IPayServiceService {

    @Autowired
    private PayServiceDao dao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    MPayServiceMapper mapper;

    @Override
    public void deleteChannel(Integer deptId) {
        PayService channel = mapper.selectById(deptId);
        Wrapper<PayService> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pids", "%[" + channel.getId() + "]%");
        List<PayService> subChannels = mapper.selectList(wrapper);
        for (PayService temp : subChannels) {
            temp.deleteById();
        }
        channel.deleteById();
    }

    public PayServiceInfo selectOneById(String payServiceId) {
        String sql = "select * from "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_info",0)+" where id= '" + payServiceId + "' limit 1";
        PayServiceInfo info = sqlMapper.selectOne(sql, PayServiceInfo.class);
        return info;
    }

    public PayServiceInfo selectOneByIdTwo(String payServiceId) {
        String sql = "select * from "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_info",0)+" where id= '" + payServiceId + "' limit 1";
        PayServiceInfo info = sqlMapper.selectOne(sql, PayServiceInfo.class);
        return info;
    }

    public List<PayServicedInfo> getPayServiceInfoList() {
        if (ShiroKit.getSessionAttr("payServiceDepartmentList") == null) {
            String sql = "select id,department from "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_grade_info",0)+" where is_upper=1 group by department order by id";
            ShiroKit.setSessionAttr("payServiceDepartmentList", sqlMapper.selectList(sql));
            return ShiroKit.getSessionAttr("payServiceDepartmentList");
        } else {
            return ShiroKit.getSessionAttr("payServiceDepartmentList");
        }
    }

    public List<PayServicedInfo> selectListByFirstLevelProject(String department, String firstLevelProject){
        String departmentFirstProjectList=department+firstLevelProject+"departmentFirstProjectList";
        if (ShiroKit.getSessionAttr(departmentFirstProjectList) == null) {
            String sql = "select id as id ,num as num ,department as department ," +
                    " first_level_project as firstLevelProject,pay_service_name as payServiceName," +
                    " pay_service_price_one as payServicePriceOne,pay_service_price_two as payServicePriceTwo ," +
                    " unit as unit ,remarks as remarks from "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_info",0)+" a where department='"+department+"' " +
                    " and first_level_project= '"+firstLevelProject+"' and is_upper = 1";
            ShiroKit.setSessionAttr(departmentFirstProjectList, sqlMapper.selectList(sql));
            return ShiroKit.getSessionAttr(departmentFirstProjectList);
        } else {
            return ShiroKit.getSessionAttr(departmentFirstProjectList);
        }
    }

    public int insert(PayServiceInfo info) {
        String sql = "INSERT INTO "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_info",0)+" (`num`,`department`, `first_level_project`, " +
                "`pay_service_name`, `pay_service_price_one`, `pay_service_price_two`, `unit`, `remarks`) VALUES" +
                "(  " + info.getNum() + " ,'" + info.getDepartment().trim() + "','" + info.getFirstLevelProject() + "'" +
                ",'" + info.getPayServiceName()+ "'," + info.getPayServicePriceOne()+ "," + info.getPayServicePriceTwo()+ "," +
                "'" + info.getUnit()+ "','" + info.getRemarks()+ "');";
        return sqlMapper.insert(sql);
    }

    public int updatePayService(PayServiceInfo info) {
        String sql ="update "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_info",0)+" a set a.department='"+info.getDepartment().trim()+"', " +
                " a.num="+info.getNum()+",a.first_level_project='"+info.getFirstLevelProject()+"',a.pay_service_name='"+info.getPayServiceName()+"', " +
                " a.pay_service_price_one="+info.getPayServicePriceOne()+",a.pay_service_price_two="+info.getPayServicePriceTwo()+", " +
                " a.unit='"+info.getUnit()+"',a.remarks='"+info.getRemarks()+"' " +
                " where id='"+info.getId()+"'";
        return sqlMapper.update(sql);
    }

    @Transactional
    public void delete(String id) {
        String sql = "delete from "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_info",0)+"  where id = '" + id + "'";
        sqlMapper.delete(sql);
    }

    @Override
    public List<PayServiceInfo> selectPageListByQuery(Page<PayServiceInfo> page, SearchPayServiceInfo searchInfo){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getTreatmentUnit())) {
            params.put("treatmentUnit", searchInfo.getTreatmentUnit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFirstLevelProjectDivsd())) {
            params.put("firstLevelProjectDivsd", searchInfo.getFirstLevelProjectDivsd());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsUpperStatus())) {
            params.put("isUpperStatus", searchInfo.getIsUpperStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPayServiceName())) {
            params.put("payServiceName", searchInfo.getPayServiceName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsDeal())) {
            params.put("isDeal", searchInfo.getIsDeal());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsTeethProject())) {
            params.put("isTeethProject", searchInfo.getIsTeethProject());
        }
        params.put("pay_service_info", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_info",0));
        return dao.getQueryPage(page, params);
    }

    public int update(PayServiceInfo info) {
        return dao.updateById(info);
    }

    public List<Map<String, Object>> list() {
        String sql = "select a.department as department from "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_grade_info",0)+" a where a.is_upper=1 group by a.department order by a.id;";
        return sqlMapper.selectList(sql);
    }
}